package cc.deng.wubo.minapp.core;

import cc.deng.wubo.minapp.beans.*;
import org.apache.shiro.crypto.hash.Sha256Hash;
import org.nutz.dao.Dao;
import org.nutz.dao.impl.FileSqlManager;
import org.nutz.dao.sql.Sql;
import org.nutz.dao.util.Daos;
import org.nutz.lang.random.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;

@Component
@Order(1)
public class SetUp implements ApplicationRunner {

    @Autowired
    Dao dao;

    @Override
    public void run(ApplicationArguments args) throws Exception {

    initSysData();
    }

    public void initSysData(){
        if (0==dao.count(SysUser.class)){
            // 创建用户
            SysUser sysUser = new SysUser();
            String slat = R.UU16();
            String src = "admin";
            String password = new Sha256Hash(src,slat).toHex();
            String userName = "admin";
            String trueName = "dalao";
            sysUser.setUserName(userName);
            sysUser.setPassword(password);
            sysUser.setSlat(slat);
            sysUser.setTrueName(trueName);
            sysUser =  dao.insert(sysUser);

            // 创建角色
            SysRole sysRole = new SysRole();
            sysRole.setName("管理员");
            sysRole.setRemarks("系统管理员");

            sysRole = dao.insert(sysRole);

            SysUserRole sysUserRole = new SysUserRole();
            sysUserRole.setRoleId(sysRole.getId());
            sysUserRole.setUserId(sysUser.getId());
            dao.insert(sysUserRole);


            SysMenu menu = new SysMenu();
            menu.setName("系统管理");
            menu.setState(1);
            menu.setIcon("menu-plugin");
            menu.setPid(-1);
            menu= dao.insert(menu);

            SysRoleMenu roleMenu = new SysRoleMenu();
            roleMenu.setMenuId(menu.getId());
            roleMenu.setRoleId(sysRole.getId());
            dao.insert(roleMenu);

            menu = new SysMenu();
            menu.setName("词库管理");
            menu.setState(0);
            menu.setIcon("menu-53");
            menu.setUrl("/power/idiom.html");
            menu.setPid(1);
            menu=dao.insert(menu);

            roleMenu = new SysRoleMenu();
            roleMenu.setMenuId(menu.getId());
            roleMenu.setRoleId(sysRole.getId());
            dao.insert(roleMenu);


        }


    }

}
